package com.canyou.utils;

import org.joda.money.Money;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 金额格式化
 *
 * @author cwk
 * @date 2020/5/18 15:42
 */
public final class FormatUtils {

    private FormatUtils() {
    }

    /**
     * 数字字符串添加千分位
     *
     * @param text
     * @return
     * @author cwk
     * @date 2020-05-05
     */
    public static String fmtMicrometer(String text) {
        DecimalFormat df = new DecimalFormat("###,##0.00");
        double number = 0.00;
        try {
            number = Double.parseDouble(text);
        } catch (Exception e) {
            number = 0.00;
        }
        return df.format(number);
    }

    /**
     * 钱数据类型添加千分位
     *
     * @param money
     * @return
     * @author cwk
     * @date 2020-05-18
     */
    public static String fmtMicrometer(Money money) {
        BigDecimal bigDecimal = BigDecimal.valueOf(0.00);
        DecimalFormat df = new DecimalFormat("###,##0.00");
        if (money != null) {
            bigDecimal = money.getAmount();
        }
        return df.format(bigDecimal);
    }

    /**
     * 数据添加千分位格式化
     *
     * @param clazz
     * @param param
     * @return
     * @author cwk
     * @date 2020-05-19
     */
    public static <T> String fmtMicrometer(Class<T> clazz, T param) {
        if (clazz.equals(Float.class)) {
            String text = String.valueOf(param);
            return fmtMicrometer(text);
        } else if (clazz.equals(Money.class)) {
            BigDecimal bigDecimal = BigDecimal.valueOf(0.00);
            DecimalFormat df = new DecimalFormat("###,##0.00");
            Money money = (Money) param;
            if (money != null) {
                bigDecimal = money.getAmount();
            }
            return df.format(bigDecimal);
        } else if (clazz.equals(BigDecimal.class)) {
            BigDecimal bigDecimal = BigDecimal.valueOf(0.00);
            DecimalFormat df = new DecimalFormat("###,##0.00");
            if (param != null) {
                bigDecimal = (BigDecimal) param;
            }
            return df.format(bigDecimal);
        }
        return fmtMicrometer(param.toString());
    }

    /**
     * 科学计数数据转为普通数据
     *
     * @param flt
     * @return
     * @author cwk
     * @date 2020-05-05
     */
    public static <T> String fmtScientific(T flt) {
        String str = String.valueOf(flt);
        BigDecimal num = new BigDecimal(str);
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(num);
    }

}
